var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShowPasswordTips: false, //选中提示输入支付密码,false隐藏、true显示
    paymentType: 1, // 支付方式 1 线下 2 微信 3 银行卡 4 余额
    orderVo: {}, //用于存储订单信息

    buthidden: false,
    amount: '', //账户余额
    amountOk: true, //余额是否可用
    ifTrue: false,
    Length: 6, //输入框个数
    isFocusOne: true, //聚焦
    ValueOne: '', //输入的内容密码
    ispassword: true, //是否密文显示 true为密文， false为明文。
    userVo: '',
    buthiddenV: false,
    ifShowV: true,
    ifbuthiddenV: true,
    flag: 0,
    statusSelect: '', //判断是否选中
    checks: [],
    comment: '您对本次服务满意吗',
    arrayChecks: '',
    //
    orderInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    let data = JSON.parse(options.data);
    console.log(data);
    that.setData({ orderInfo: data });
  },
  //充值
  pushmoney: function() {
    var that = this;
    wx.navigateTo({
      url: '/pages/mine/paymentCard'
    });
  },
  //微信支付
  paymentWx: function() {
    var that = this;
    that.setData({ paymentType: 2 });
  },
  //银行卡支付
  paymentUpper: function() {
    var that = this;
    that.setData({ paymentType: 3 });
  },
  //线下支付
  paymentLower: function() {
    var that = this;
    that.setData({ paymentType: 1 });
  },
  //余额支付
  paymentMoney: function() {
    var that = this;
    if (that.data.amountOk) {
      that.setData({ paymentType: 4 });
    }
  },
  bindFormSubmit: function() {
    var that = this;

    if (that.data.paymentType == 4 && that.data.ValueOne == '') {
      that.getPaymetFlag(); //判断支付密码是否设置
    } else if (that.data.paymentType == 4 && that.data.ValueOne.length != 6) {
      wx.showToast({
        title: '密码长度有误',
        icon: 'none'
      });
    } else {
      that.setData({ buthidden: true });
      that.onMineInfo();
    }
  },

  changeColor1: function() {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 1,
      checks: [
        {
          name: '货量少',
          value: '0',
          checked: false
        },
        {
          name: '货质一般',
          value: '1',
          checked: false
        },
        {
          name: '回收频率低',
          value: '2',
          checked: false
        },
        {
          name: '不守信用',
          value: '3',
          checked: false
        }
      ],
      comment: '一般用户'
    });
  },
  changeColor2: function() {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 2,
      checks: [
        {
          name: '货量少',
          value: '0',
          checked: false
        },
        {
          name: '货质一般',
          value: '1',
          checked: false
        },
        {
          name: '回收频率低',
          value: '2',
          checked: false
        },
        {
          name: '不守信用',
          value: '3',
          checked: false
        }
      ],
      comment: '一般用户'
    });
  },
  changeColor3: function() {
    var that = this;
    that.setData({
      flag: 3,
      ifbuthiddenV: false,
      checks: [
        {
          name: '货量较多',
          value: '0',
          checked: false
        },
        {
          name: '货质优良',
          value: '1',
          checked: false
        },
        {
          name: '回收有频率',
          value: '2',
          checked: false
        },
        {
          name: '态度较好',
          value: '3',
          checked: false
        }
      ],
      comment: '优质用户'
    });
  },
  changeColor4: function() {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 4,
      checks: [
        {
          name: '货量较多',
          value: '0',
          checked: false
        },
        {
          name: '货质优良',
          value: '1',
          checked: false
        },
        {
          name: '回收有频率',
          value: '2',
          checked: false
        },
        {
          name: '态度较好',
          value: '3',
          checked: false
        }
      ],
      comment: '优质用户'
    });
  },
  changeColor5: function() {
    var that = this;
    that.setData({
      ifbuthiddenV: false,
      flag: 5,
      checks: [
        {
          name: '货量大',
          value: '0',
          checked: false
        },
        {
          name: '货质优',
          value: '1',
          checked: false
        },
        {
          name: '回收频率高',
          value: '2',
          checked: false
        },
        {
          name: '态度好',
          value: '3',
          checked: false
        }
      ],
      comment: 'VIP用户'
    });
  },

  clicks: function(e) {
    let index = e.currentTarget.dataset.index;
    let arrs = this.data.checks;
    let array = new Array();
    let text = e.currentTarget.dataset.text;
    if (arrs[index].checked == false) {
      arrs[index].checked = true;
      array.push(text);
    } else {
      arrs[index].checked = false;
    }
    this.setData({
      checks: arrs
    });
  },

  // 提交清空当前值
  bindSubmit: function() {
    var that = this;
    var checks = that.data.checks;
    var array = new Array();

    for (var i = 0; i < checks.length; i++) {
      if (checks[i].checked == true) {
        array.push(checks[i].name);
      }
    }
    that.setData({ arrayChecks: array });
    console.log(array);
    console.log(that.data.arrayChecks);
    if (array.length <= 0) {
      wx.showToast({
        title: '评价类别必选',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    wx.showLoading({ title: '加载中' });
    that.setData({ buthiddenV: true });
    wx.request({
      method: 'POST',
      url: 'https://www.zaishengwang.com/wx/toSaveEvaluate.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: that.data.orderVo.userId,
        sendUserId: that.data.orderVo.receiptId,
        orderId: that.data.orderVo.orderNumber,
        score: that.data.flag,
        label: that.data.arrayChecks
      },
      success: function(res) {
        wx.hideLoading();
        var c = 2;
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1500
        });
        var intervalId = setInterval(function() {
          c = c - 1;
          if (c == 0) {
            clearInterval(intervalId);
            that.setData({ ifShowV: true });
            wx.reLaunch({
              url: 'home?finish=1'
            });
          }
        }, 1000);
      },
      fail: function(error) {}
    });
  },

  //评价隐藏
  cancelV: function() {
    var that = this;
    that.setData({ ifShowV: true });
    wx.reLaunch({
      url: 'home?finish=1'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getAccout();
  },
  getAccout() {
    var that = this;
    wx.showLoading({
      title: '加载中'
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'accountInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 100) {
          console.log(res);
          var amount = res.data.result.amount;
          that.setData({ amount: amount });
          if (amount > that.data.orderInfo.moneyTotal) {
            that.setData({ amountOk: true });
          } else {
            that.setData({ amountOk: false, paymentType: 1 });
          }
        }
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  getPaymetFlag() {
    var that = this;
    wx.showLoading({
      title: '加载中'
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'payPassInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code != 100) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '请前往设置支付密码',
            confirmText: '去设置',
            confirmColor: '#56BDE0',
            success(res) {
              if (res.confirm) {
                wx.navigateTo({ url: '../mine/accountSetting' }); //
              }
            }
          });
        } else {
          that.setData({ ifShowPasswordTips: true });
        }
      },
      fail: function(error) {
        console.log(error);
      }
    });
  },
  onFocusOne: function(e) {
    //输入密码
    var that = this;
    console.log(e.detail.value);
    var inputValue = e.detail.value.trim();
    that.setData({
      ValueOne: inputValue
    });
    if (e.detail.value.length >= 6) {
      that.setData({ ifShowPasswordTips: false });
      that.bindFormSubmit();
    }
  },

  onTapOne: function(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.index);
    var that = this;
    that.setData({
      isFocusOne: true
    });
  },

  onRightCancel: function() {
    //关闭支付键盘
    var that = this;
    that.setData({ ValueOne: '', ifShowPasswordTips: false });
  },

  //忘记密码
  goCheckSetPaymentPassword: function() {
    var that = this;
    var wxType = that.data.userVo.wxType;
    var phone = that.data.userVo.telephone;
    wx.navigateTo({
      url:
        '../mine/checkSetPaymentPassword?wxType=' + wxType + '&phone=' + phone
    });
  },
  //余额支付
  onMineInfo: function() {
    var that = this;
    wx.showLoading({ title: '支付中' });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'orderPay',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId, //打包站ID
        id: that.data.orderInfo.id, //订单id
        paymentType: that.data.paymentType,
        orderStatus: 5,
        moneyTotal: that.data.orderInfo.moneyTotal,
        password: that.data.ValueOne,
        supplierId: that.data.orderInfo.supplierId //供应商ID
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.code === 100) {
          that.setData({
            ValueOne: ''
          });
          // wx.showToast({
          //   title: '支付成功',
          //   icon: 'success',
          //   duration: 1000
          // })
          // setTimeout(function() {
          wx.redirectTo({
            url:
              'paysucc?allTotal=' +
              that.data.orderInfo.moneyTotal +
              '&paymentType=' +
              that.data.paymentType +
              '&receiptContacts=' +
              that.data.orderInfo.supplierName
          });
          // }, 1500)
        } else {
          that.setData({
            ValueOne: ''
          });
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
          that.setData({ buthidden: false });
        }
      },
      fail: function(error) {
        console.log(error);
        that.setData({
          ValueOne: ''
        });
      }
    });
  }
});
